﻿@using Citi.Scheduler.Domain
@using Citi.Scheduler.WebUi.Models
@using GridMvc.Html
@model IEnumerable<RoutineExecutionModel>
@functions{
    //var showOptions = ViewBag.ShowOptions != null && ViewBag.ShowOptions == true;
    private bool showOptions;
}
@helper CustomCategory(RoutineExecutionModel item)
    {
        showOptions = ViewBag.ShowOptions != null && ViewBag.ShowOptions == true;

    @(showOptions ? Html.ActionLink(item.Routine.Category.Description, "Calendar", "Routine", new { categoryId = item.Routine.CategoryId }, null) : Html.DisplayFor(modelItem => item.Routine.Category.Description))
}
@helper CustomRoutineName(RoutineExecutionModel item)
    {
    @(showOptions ? Html.ActionLink(item.Routine.Name, "Calendar", "Routine", new { routineId = item.RoutineId }, null) : Html.DisplayFor(modelItem => item.Routine.Name))
}
@helper CustomActions(RoutineExecutionModel item)
    {
        if (showOptions)
        {
            @*@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "icon-pencil"})
            <text></text>*@
            @Html.ActionLink("Details", "Index", "RoutineExecutionHistory", new { routineExecutionId = item.Id }, new { @class = "icon-eye-open" })
@*                         <text></text>
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "icon-remove" })
*@            if (item.RoutineExecutionHistories.Any() && item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.InProgress)
            {
               <text></text>
                @Html.ActionLink("Real-Time", "RealTime", new { routineExecutionId = item.Id }, new { @class = "icon-dashboard" })
            }
             if (item.RoutineExecutionHistories.Any() && (item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.PredecessorNotScheduled
                 || item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.PredecessorRoutineWithError
                 || item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.Scheduled
                 || item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.WaitingFreeMachine
                 || item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.WaitingPredecessorRoutine
                 || item.RoutineExecutionHistories.Last().Status == RoutineExecutionStatus.WaitingSpecificFreeMachine))
            {
               <text>
                @Html.ActionLink("Cancel", "Cancel", new { id = item.Id }, new { @class = "icon-remove" })
                 </text>
            }
        }
}
@Html.Grid(Model).Columns(columns =>
{
    columns.Add(c => c.Id, true);
    columns.Add(c => c.Routine.Category.Name).Titled("Category").Filterable(true)
        .Encoded(false).Sanitized(false).RenderValueAs(o => CustomCategory(o));
    columns.Add(c => c.Routine.Name).Titled("RoutineName").Filterable(true)
        .Encoded(false).Sanitized(false).RenderValueAs(o => CustomRoutineName(o));
    columns.Add(c => c.ScheduledDateTime).Titled("Scheduled Date").Filterable(true);
    columns.Add(c => c.RoutineId).Titled("Status").Filterable(true)
        .RenderValueAs(o => o.RoutineExecutionHistories.Any() ? string.Format("{0} at {1}", o.RoutineExecutionHistories.OrderBy(x => x.Date).Last().Status.GetDescription(),
            o.RoutineExecutionHistories.OrderBy(x => x.Date).Last().Date.ToString("dd/MM/yyyy HH:mm")) : string.Empty);
    columns.Add(c => c.StartsOn).Titled("Started On").Filterable(true);
    columns.Add(c => c.EndsOn).Titled("Ended On").Filterable(true);
    columns.Add(c => c.MachineId).Titled("Machine").Filterable(true).RenderValueAs(o => o.Machine != null ? o.Machine.HostName : string.Empty); ;
    columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => CustomActions(o));
    //columns.Add(o => o.LibraryId)
    //    .Titled("Actions")
    //    .SetWidth(200)
    //    .Encoded(false)
    //    .Sanitized(false)
    //    .RenderValueAs(o => CustomRenderingOfColumn(o));
}).WithPaging(15).Sortable(true)